package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.StandardMapper;
import com.zhijian.medical.entity.po.Standard;
import com.zhijian.medical.entity.vo.request.StandardSearchVo;
import com.zhijian.medical.entity.vo.request.StandardSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.StandardVo;
import com.zhijian.medical.service.StandardService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 标准业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-04-29 17:44:47
 */
@Slf4j
@Service
public class StandardServiceImpl implements StandardService {

	@Autowired
    private StandardMapper mapper;

    @Override
    public Standard selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public StandardVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(Standard standard) {
        Date now = new Date();
        standard.setCreateTime(now);
        standard.setUpdateTime(now);
        return mapper.insert(standard) > 0 ? standard.getId() : 0L;
    }

    @Override
    public boolean update(Standard standard) {
        Date now = new Date();
        standard.setUpdateTime(now);
        return mapper.updateByPrimaryKey(standard) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(Standard record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(StandardSaveOrUpdateVo saveOrUpdateVo) {
        Standard standard = new Standard();
        BeanUtils.copyProperties(saveOrUpdateVo, standard);
        return this.insert(standard) != 0;
    }

    @Override
    public boolean modify(StandardSaveOrUpdateVo saveOrUpdateVo) {
        Standard old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        Standard standard = new Standard();
        BeanUtils.copyProperties(saveOrUpdateVo, standard);
        return this.update(standard);
    }

    @Override
    public StandardVo selectOne(StandardSearchVo standardSearchVo) {
        return mapper.selectOne(standardSearchVo);
     }

    @Override
    public Integer count(StandardSearchVo standardSearchVo) {
        return mapper.count(standardSearchVo);
    }

    @Override
    public List<StandardVo> selectList(StandardSearchVo standardSearchVo) {
        return mapper.selectList(standardSearchVo);
    }

    @Override
    public Page<StandardVo> selectPage(StandardSearchVo standardSearchVo) {
        AssertUtil.check(Objects.isNull(standardSearchVo.getPageNum()) || Objects.isNull(standardSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(standardSearchVo);
        //分页
        Page<StandardVo> page = new Page<>(true, count, standardSearchVo.getPageNum(), standardSearchVo.getPageSize());
        standardSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<StandardVo> responseVos = this.selectList(standardSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<Standard> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(StandardSearchVo standardSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(standardSearchVo);
    }
    
}